package com.estate.system.service.message;
import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Message;
import com.twilio.type.PhoneNumber;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class TwilioSmsSender {

    // 您的Twilio凭证
    private static final String ACCOUNT_SID = "SK2b9176663130a7600cab2869c46bb960";
    private static final String AUTH_TOKEN = "IICLBrLZgYiXsiKa4pxgFNAPZfCjVnv5";

    private static final Logger log = LoggerFactory.getLogger(TwilioSmsSender.class);

    public static void main(String[] args) {
        // 初始化Twilio客户端
        Twilio.init(ACCOUNT_SID, AUTH_TOKEN);

        // 发送短信
        boolean success = sendSMS(
                //"+61413027079",  // 收件人手机号
                "+8618860919040",
                "+61483983702",    // 您的Twilio电话号码
                "This is a test SMS from Twilio"
        );

        if (success) {
            log.info("短信发送成功！");
        } else {
            log.error("短信发送失败！");
        }
    }

    //发送消息
    public boolean send(String toPhoneNumber, String message) {
        // 初始化Twilio客户端
        Twilio.init(ACCOUNT_SID, AUTH_TOKEN);

        // 发送短信
        boolean success = sendSMS(
                "+61" + toPhoneNumber,  // 收件人手机号
                "+61483983702",    // 您的Twilio电话号码
                message
        );

        if (success) {
            log.info("短信发送成功！");
            return true;
        } else {
            log.error("短信发送失败！");
            return false;
        }
    }

    //写一个批量发送短信
    public void sendBatch(Map<String, String> messages) {
        // 批量发送短信
        for (Map.Entry<String, String> entry : messages.entrySet()) {
            String toPhoneNumber = entry.getKey();
            String message = entry.getValue();
            send(toPhoneNumber, message);
        }
    }

    /**
     * 发送短信
     * @param toPhoneNumber 收件人手机号
     * @param fromPhoneNumber 发件人Twilio电话号码
     * @param message 短信内容
     * @return 发送是否成功
     */
    public static boolean sendSMS(String toPhoneNumber, String fromPhoneNumber, String message) {
        try {
            // 创建并发送短信
            Message twilioMessage = Message.creator(
                    new PhoneNumber(toPhoneNumber),  // 收件人
                    new PhoneNumber(fromPhoneNumber), // 发件人（您的Twilio号码）
                    message                          // 短信内容
            ).create();

            // 打印发送结果
            System.out.println("=== 短信发送详情 ===");
            System.out.println("短信SID: " + twilioMessage.getSid());
            System.out.println("状态: " + twilioMessage.getStatus());
            System.out.println("收件人: " + twilioMessage.getTo());
            System.out.println("发件人: " + twilioMessage.getFrom());
            System.out.println("内容: " + twilioMessage.getBody());
            System.out.println("时间: " + twilioMessage.getDateCreated());
            System.out.println("错误码: " + twilioMessage.getErrorCode());
            System.out.println("错误信息: " + twilioMessage.getErrorMessage());

            // 检查发送状态
            return "delivered".equals(twilioMessage.getStatus().toString()) ||
                    "sent".equals(twilioMessage.getStatus().toString()) ||
                    "queued".equals(twilioMessage.getStatus().toString());

        } catch (Exception e) {
            log.error("发送短信时发生异常: " + e.getMessage());
            e.printStackTrace();
            return false;
        }
    }
}